import VueRouter from "vue-router";

//引入要使用的组件
import Login from '../views/Login'
import Home from '../views/Home'
    //二级路由
    import Welcome from '../views/Welcome'
    import Users from '../views/user/Users'
    import Rights from '../views/power/Rights'
    import Roles from '../views/power/Roles'
    //<---------------商品分类---------------->
    import GoodsFenlei from '../views/goods/GoodsFenlei'

//创建一个路由器
const router = new VueRouter({
    routes:[
        {path:'/',redirect:'/login'},
        {path:'/login',component:Login,meta:{title:'登录'}},
        {
            path:'/home',
            component:Home,
            meta:{title:'主页'},
            redirect:'/welcome',
            children:[
                { path:"/welcome",name:"welcome",component:Welcome},
                { path:"/users",name:"users",component:Users},
                { path:"/rights",name:"rights",component:Rights},
                { path:'/roles',name:"roles",component:Roles, meta:{title:'角色列表'}},
                { path:'/categories',name:"categories",component:GoodsFenlei,meta:{title:'商品分类'}},
            ]
        },
    ]
})

router.beforeEach((to,from,next)=>{
    if(to.path == '/login') return next();
    const tokenSt = window.sessionStorage.getItem('token')
    if(!tokenSt) return next("/login")
    next()
})
router.afterEach(to=>{
    if(to.meta.title){
        document.title = to.meta.title
    }else{
        document.title = "爱你！！！"
    }
})

export default router